package com.example.ex2.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.ex2.common.CommonResponse;
import com.example.ex2.domain.Item;
import com.example.ex2.domain.Returninfo;
import com.example.ex2.domain.User;
import com.example.ex2.persistence.ItemMapper;
import com.example.ex2.persistence.ReturninfoMapper;
import com.example.ex2.persistence.UserMapper;
import com.example.ex2.service.ReturninfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Timer;

@Service
public class ReturninfoServiceImpl implements ReturninfoService {
    @Autowired
    private ReturninfoMapper returninfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ItemMapper itemMapper;
    public CommonResponse<List<Returninfo>> getallinfos(){

        List<Returninfo> returninfoList = returninfoMapper.getallinfos();
        String URL = "http://localhost:8080/infos";
        if(returninfoList!=null){
            return CommonResponse.createForSuccess(returninfoList,URL);
        }
        else  return CommonResponse.createForError("记录为空！",URL);
    }

    public CommonResponse<List<Returninfo>> getReturninfos(){

        List<Returninfo> returninfoList = returninfoMapper.getReturninfos();
        String URL = "http://localhost:8080/returninfos";
        if(returninfoList!=null){
            return CommonResponse.createForSuccess(returninfoList,URL);
        }
        else  return CommonResponse.createForError("归还物品清单为空！",URL);
    }

    public CommonResponse<Returninfo> getReturninfoByID(int id){
        Returninfo returninfo1 = returninfoMapper.getReturninfosById(id);
        String URL = "http://localhost:8080/returninfos/"+id;
        if(returninfo1!=null){
            return CommonResponse.createForSuccess(returninfo1,URL);
        }
        else  return CommonResponse.createForError("该id对应的物品还未归还还未归还！",URL);

    }


    public CommonResponse<Returninfo> modifyReturninfo(Returninfo returninfo){
        String username = returninfo.getUsername();
        String itemname = returninfo.getItemname();
        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        QueryWrapper<Item> queryWrapper1 =new QueryWrapper<>();
        queryWrapper1.eq("name",itemname);
        Item item = itemMapper.selectOne(queryWrapper1);
        String URL = "http://localhost:8080/returninfos/";
        if(user!=null&&item!=null){
            int user_id = user.getUser_id();
            int item_id = item.getItem_id();
            boolean b = returninfoMapper.updateReturninfo(returninfo.getId(), user_id, item_id, returninfo.getReturn_state(), returninfo.getBorrow_time(), returninfo.getReturn_time());

            if(b){
                return CommonResponse.createForSuccess("修改归还信息成功",returninfo,URL);
            }
            else return CommonResponse.createForError("修改归还信息失败！",URL);
        }
        else return CommonResponse.createForError("用户或物品不存在！",URL);


    }

    public CommonResponse<Returninfo> addReturninfo(Returninfo returninfo){
        int user_id1 = returninfo.getUser_id();
        int item_id1 = returninfo.getItem_id();
        int id = returninfo.getId();
        Returninfo returninfo1 = returninfoMapper.getReturninfosByid(id);
//        QueryWrapper<Returninfo> queryWrapper =new QueryWrapper<>();
//        queryWrapper.eq("user_id",user_id1).eq("item_id",item_id1);
//        Returninfo returninfo1 = returninfoMapper.getReturninfosByUser_idAndItem_id(user_id1,item_id1);
        Date date =new Date();
        QueryWrapper<Item> queryWrapper1 =new QueryWrapper<>();
        queryWrapper1.eq("item_id",item_id1);
        Item item = itemMapper.selectOne(queryWrapper1);
        int remain_num = item.getRemain_num();
        String URL = "http://localhost:8080/returninfos/";
        if(returninfo1!=null){
            boolean b = returninfoMapper.addReturninfo(returninfo1.getId(), user_id1, item_id1, 1, returninfo1.getBorrow_time(), date);
            if(b){
                if(remain_num+1<=item.getTotal_num()){
                    item.setRemain_num(remain_num+1);
                    itemMapper.update(item,queryWrapper1);
                    return CommonResponse.createForSuccess("新增归还信息成功",URL);
                }
            }
            return CommonResponse.createForError("新增归还信息失败！",URL);
        }else return CommonResponse.createForError("用户或物品不存在！",URL);

    }

    public CommonResponse<Returninfo> deleteReturninfo(int id){
        String URL = "http://localhost:8080/returninfos/"+id;
        if(returninfoMapper.deleteReturninfosById(id)!=null) {
            returninfoMapper.deleteById(id);
            return CommonResponse.createForSuccess("删除归还信息成功", URL);
        }
        else {
            return CommonResponse.createForError("没有该id对应的归还记录，删除失败！",URL);}

    }
}
